﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Model;
using ViewModel.admin;

namespace WebApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            Mapper.Initialize(x => {

                x.CreateMap<sys_admin, sys_adminVm>();
                x.CreateMap<sys_adminVm, sys_admin>();

                x.CreateMap<sys_admin_groupVm, sys_admin_group>();
                x.CreateMap<sys_admin_group, sys_admin_groupVm>();

                x.CreateMap<sys_admin_group_urlVm, sys_admin_group_url>();
                x.CreateMap<sys_admin_group_url, sys_admin_group_urlVm>();

                x.CreateMap<sys_admin_urlVm, sys_admin_url>();
                x.CreateMap<sys_admin_url, sys_admin_urlVm>();

            }
            );

            services.AddTransient<EFContext>();//注入
            EFContext.ConnectionString = Configuration.GetConnectionString("wechatauto");
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, x =>
             {
                 //x.Cookie.Name = "_AdminTicketCookie";
                 x.LoginPath = new PathString("/Home/Login");
                 x.AccessDeniedPath = new PathString("/Home/Error");
             });
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            app.UseAuthentication();//添加认证中间件
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
